package com.drg.center.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.drg.center.dao.*;
import com.drg.center.entity.*;
import com.drg.center.entity.param.DiagnosisGroupPageDTO;
import com.drg.center.entity.param.GroupPredictionDTO;
import com.drg.center.entity.result.*;
import com.drg.center.service.BaseDealService;
import com.drg.center.service.DrgPredictionService;
import com.guaike.core.entity.result.PageEntity;
import com.guaike.core.entity.result.Result;
import com.guaike.core.utils.ListUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Slf4j
@Service
@AllArgsConstructor
public class DrgPredictionServiceImpl implements DrgPredictionService {

    private final DictDiagnosisDAO dictDiagnosisDAO;
    private final DictDiagnosisHisDAO dictDiagnosisHisDAO;
    private final DictAdrgDAO dictAdrgDAO;
    private final DictMdcDAO dictMdcDAO;
    private final DictOperationDAO dictOperationDAO;
    private final BaseDealService baseDealService;


    @Override
    public Result<PageEntity<DiagnosisGroupVO>> diagnosisGroupPage(DiagnosisGroupPageDTO param) {
        Page<DictDiagnosisPO> poPage = dictDiagnosisDAO.findDiagnosisGroupPage(param);
        PageEntity<DiagnosisGroupVO> result = new PageEntity<>();
        List<DiagnosisGroupVO> list = new ArrayList<>();
        poPage.getRecords().forEach(po -> {
            DiagnosisGroupVO vo = new DiagnosisGroupVO();
            BeanUtils.copyProperties(po, vo);
            DictMdcPO mdcPO = dictMdcDAO.findOne(DictMdcPO::getMdcCode, po.getMdcCode());
            vo.setMdcName(mdcPO.getMdcName());
            DictAdrgPO adrgPO = dictAdrgDAO.findOne(DictAdrgPO::getAdrgCode, po.getAdrgCode());
            vo.setAdrgName(adrgPO.getAdrgName());
            list.add(vo);
        });
        result.setTotal(poPage.getTotal());
        result.setList(list);
        return Result.success().data(result).build();
    }

    @Override
    public Result<List<MdcVO>> searchMDC(String search) {
        List<DictMdcPO> pos = dictMdcDAO.searchMDC(search);
        List<MdcVO> vos = ListUtil.copyList(pos, MdcVO.class);
        return Result.success().data(vos).build();
    }

    @Override
    public Result<List<AdrgVO>> searchADRG(String search) {
        List<DictAdrgPO> pos = dictAdrgDAO.searchADRG(search);
        List<AdrgVO> vos = ListUtil.copyList(pos, AdrgVO.class);
        return Result.success().data(vos).build();
    }

    @Override
    public Result<List<DiagnosisVO>> searchDiagnosis(String search) {
        List<DictDiagnosisHisPO> pos = dictDiagnosisHisDAO.searchDiagnosis(search);
        List<DiagnosisVO> vos = ListUtil.copyList(pos, DiagnosisVO.class);
        return Result.success().data(vos).build();
    }

    @Override
    public Result<List<OperationVO>> searchOperation(String search) {
        List<DictOperationPO> pos = dictOperationDAO.searchOperation(search);
        List<OperationVO> vos = ListUtil.copyList(pos, OperationVO.class);
        return Result.success().data(vos).build();
    }

    @Override
    public Result<GroupPredictionVO> groupPrediction(GroupPredictionDTO param) {
        GroupPredictionVO vo = baseDealService.groupPrediction(param.getSex(), param.getAge(), param.getAgeDay(), Objects.isNull(param.getMedicareType()) ? "" : param.getMedicareType().toString(), param.getTotalCost()
                , param.getInpatientDays(), param.getDiagnosisCode(), param.getOperationCode(), param.getDiagnosisCodeList(), param.getOperationCodeList());
        return Result.success().data(vo).build();
    }
}
